package controller

import (
	"gin-epg/internal/app/common/rsp"
	"gin-epg/internal/app/param"
	"gin-epg/internal/app/service"
	"github.com/gin-gonic/gin"
)

func CreateEpgChannelAlias(c *gin.Context) {
	//定义一个EpgChannel变量
	var epgChannelAlias param.AddEpgChannelAliasParam
	//将调用后端的request请求中的body数据根据json格式解析到EpgChannel结构变量中
	c.BindJSON(&epgChannelAlias)
	//将被转换的epgChannel变量传给service层的CreateEpgChannel方法，进行EpgChannel的新建
	err := service.CreateEpgChannelAlias(&epgChannelAlias)
	//判断是否异常，无异常则返回包含200和更新数据的信息
	if err != nil {
		rsp.Error(c, err.Error())
	} else {
		rsp.Success(c, "新增成功", epgChannelAlias)
	}
}

func GetEpgChannelAliasList(c *gin.Context) {
	todoList, err := service.GetAllEpgChannelAlias()
	if err != nil {
		rsp.Error(c, err.Error())
	} else {
		rsp.Success(c, "请求成功", todoList)
	}
}

func UpdateEpgChannelAlias(c *gin.Context) {
	id, ok := c.Params.Get("id")
	if !ok {
		rsp.Error(c, "无效的id")
	}
	epgChannelAlias, err := service.GetEpgChannelAliasById(id)
	if err != nil {
		rsp.Error(c, err.Error())
		return
	}
	c.BindJSON(&epgChannelAlias)
	if err = service.UpdateEpgChannelAlias(epgChannelAlias); err != nil {
		rsp.Error(c, err.Error())
	} else {
		rsp.Success(c, "更新成功", epgChannelAlias)
	}
}

func DeleteEpgChannelAliasById(c *gin.Context) {
	id, ok := c.Params.Get("id")
	if !ok {
		rsp.Error(c, "无效的id")
	}
	if err := service.DeleteEpgChannelAliasById(id); err != nil {
		rsp.Error(c, err.Error())
	} else {
		rsp.Success(c, "删除成功", id)
	}
}
